﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Content/Master.Master" Inherits="System.Web.Mvc.ViewPage<SCS.Core.ViewModels.UserViewModels>" %>
<%@ Import Namespace="SCS.Core" %>
<%@ Import Namespace="SCS.Core.Models" %>
<%@ Import Namespace="SCS.Core.ViewModels" %>
<%@ Import Namespace="SCS.Core.Controllers" %>
<%@ Import Namespace="SCSService" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	ListUsers
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <script type="text/javascript">

        function SaveStatus(dropDownList, id) {
            var selectedReport = dropDownList.options[dropDownList.selectedIndex];
            var selects = selectedReport.value;
            actionChangeStatus(selects, id);
        }
        function actionChangeStatus(values, id) {
            var url = '<%=Url.Action("ChangeUserStatus","User")%>' + '/' + id + '/' + values;
            $.getJSON(url, function (outStr) {
                $('#statusID' + id).val(outStr);
            });
        }
        function actionResetPassword(id) {
            var url = '<%=Url.Action("ResetPassword","User")%>' + '/' + id;
            $.getJSON(url, function (outStr) {
                //$('#statusID' + id).val(outStr);
                alert(outStr);
            });
        } 
</script> 
     <% using (Html.BeginForm("ListUsers", "User", FormMethod.Post, new { UserName = "sendFileForm", Email = "sendFileForm", UserStatus = "sendFileForm", enctype = "multipart/form-data" }))
        {%>
    <div class="clear"></div>
    <div class="column full">
    <div class="box ">
		<h2 class="box-header">	List User </h2>
		<div class="box-content">

                    <div id="grid">
        
		                
				    						               
				                <table class="display">
                                <thead>
					                <tr>
						                <th>
                                             <a href="#" class="<%= UserController.GetGridThClass(ViewData, "UserID") %>" 
										                onclick="<%= UserController.GetGridThOnClick(ViewData, "UserID")%>">
								                UserID</a>
                                        </th>                                        					               
						                <th>
							                <a href="#" class="<%= UserController.GetGridThClass(ViewData, "UserName") %>" 
										                onclick="<%= UserController.GetGridThOnClick(ViewData, "UserName")%>">
								                UserName</a>
						                </th>
						
                                        <th>
                                            <a href="#" class="<%= UserController.GetGridThClass(ViewData, "Email") %>" 
										                onclick="<%= UserController.GetGridThOnClick(ViewData, "Email")%>">
								                Email</a>                                        
                                        </th>
                                 		
                                        <th>
                                            Status                                    
                                        </th>		
                                        							                
                                        <th>
                                              Role
                                        </th>                                  
                                        
                                        <th></th>
					                </tr>
                                    <tr>
                                        <td>
                                            
                                        </td>
                                        <td>
                                            <%=Html.TextBoxFor(m => m.UserName, new { @class = "form-field hafl", @maxlength = "100" })%>
                                        </td>
                                                                           
                                        <td>
                                           <%=Html.TextBoxFor(m => m.Email, new { @class = "form-field hafl", @maxlength = "200" })%>
                                        </td>

                                        <td>
                                            <%
                                                List<SelectListItem> statusSearch = new List<SelectListItem>();
                                                statusSearch.AddRange(new SelectListItem[] { 
                                                new SelectListItem{ Text = "active", Value = "1" },
                                                new SelectListItem{ Text = "unactive", Value = "0" }
                                                });
                                                %>

                                                <%=Html.DropDownListFor(m => m.UserStatus, statusSearch)%> 
                                        </td>

                                        <td>
                                            
                                        </td>
                                                                   
                                        <td>
                                            
                                            <input type="submit" value="Search" class="button themed"/>
                                            <%= Html.ActionLink("Create New User", "CreateUser", null, new { @class = "button themed" })%>
                                        </td>
                                    </tr>
                                </thead>
                                <% if (Model.UserItems.Count() == 0) { %>
			                        <tbody>
                                        <tr>
                                            <td colspan="6">
                                                Not found User
                                            </td>
                                        </tr>
                                    </tbody>
		                        <% } else { %>
                                <tbody>
					                <% foreach (var item in Model.UserItems) { %>
				                        
						                <tr>						                
                                            <td>
                                                <%= Html.Encode(item.UserID) %>
                                            </td>
							                <td>
								                <%= Html.Encode(item.UserName) %>
							                </td>														               
											
                                            <td>
                                                <%=Html.Encode(item.Email) %>                                                
                                            </td>

                                            <td>
                                                <%
                                                    bool newStatus=false;
                                                    if (item.UserStatus == 0)
                                                    {
                                                        newStatus=true;
                                                    }
                                                    else if (item.UserStatus == 1)
                                                    {
                                                        newStatus=false;
                                                    }
                                                %>
                                                <%
                                                List<SelectListItem> statusDrop = new List<SelectListItem>();
                                                statusDrop.AddRange(new SelectListItem[] { 
                                                new SelectListItem{Selected=newStatus, Text = "active", Value = "1" },
                                                new SelectListItem{Selected=newStatus, Text = "unactive", Value = "0" }
                                                });
                                                %>
                                              
                                               <%= Html.DropDownList("statusID" + item.UserID, statusDrop, new { onchange = "SaveStatus(this," + item.UserID + ")" })%>
                                            </td>
                                           
                                           <td>
                                                <% 
                                                    string roleName = "";
                                                    if (item.RoleID != null)
                                                    {
                                                        roleName = UserModel.GetRoleByID((int)item.RoleID).RoleName;
                                                    }
                                                %>
                                                <%=roleName %>
                                           </td>
                                            
                                            <td> 
                                                
                                               <a href="#" onclick="actionResetPassword(<%=item.UserID %>);" >Reset password</a> | 
                                               <%= Html.ActionLink("Edit", "EditUser", new { id = item.UserID })%> | 
                                                								                
                                                <%
                                                    string returnUrl = Request.Url.ToString();
                                                 %>
                                                <%= Html.ActionLink("Delete", "DeleteUser", new { id = item.UserID, returnUrl = returnUrl }, new { onclick = "return confirm('Delete User:" + item.UserID + " ?');" })%>
                                            </td>
						                </tr>
                                            <script type="text/javascript">
                                                
                                            </script>
                                            
                                             
					                <% } %>
					            </tbody>
                                 <% } %>
				                </table>
			                <!-- data -->
                                <% if (Model.UserItems.Count() > 0) { %>
			                       <% Html.RenderPartial("~/Views/Commont/Paging.ascx"); %>
		                        <% } %>
		               
	                </div>  <!-- grid -->	
                
               
	    </div>
	</div>
    <div class="clear"></div>
    </div>    
    <% } %>
</asp:Content>
